﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using LS.FrmUI.Dal;
using LS.FrmUI.Common;
using System.Reflection;

namespace LS.FrmUI
{
    public partial class FrmMain : Form
    {
        public FrmMain()
        {
            InitializeComponent();
            CrateFileInDiskD();

        }
        private void CrateFileInDiskD()
        {
            if (!Directory.Exists(@"D:\物流管理系统文件"))
            {
                Directory.CreateDirectory(@"D:\物流管理系统文件");
            }
        }
        AttDal dal = new AttDal();
        private void btnImportTodayData_Click(object sender, EventArgs e)
        {
            /*
             * 附件销售明细导出.xlsx
             * 检测工单明细.xlsx
             * 维修工单.xlsx
             */
            if (dal.IsHasImport())
            {
                txtLog.AppendText(DateTime.Now + "今天的数据已经导入，如需再次导入请点击清理数据。\r\n");

                return;
            }
            btnImportTodayData.Enabled = false;
            DirectoryInfo dirInfo = new DirectoryInfo(@"D:\物流管理系统文件");
            FileInfo[] fileInfo = dirInfo.GetFiles("*.xlsx");
            if (!fileInfo.Any())
            {
                    txtLog.AppendText(DateTime.Now + ":导入失败,请检查【D:\\物流管理系统文件】下文件是否合法\r\n");
            }
            foreach (var item in fileInfo)
            {
                //DataSet ds = OfficeUtil.ExcelToDataSetByGjp(item.FullName); 
                bool b = dal.ImportToDBFromFile(DBHelper.connectionString, item.FullName);
                if (b)
                {
                    txtLog.AppendText(DateTime.Now + ":导入成功。" + item.FullName+"\r\n");
                }
                else
                {
                    txtLog.AppendText(DateTime.Now + ":导入失败!!!" + item.FullName + "\r\n");
                }
            }
            btnImportTodayData.Enabled = true;
        }

        private void btnChartOutPut_Click(object sender, EventArgs e)
        {
            FrmChartOutPut f = new FrmChartOutPut();
            f.Show();
            this.Hide();
            //new FrmChartOutPut().ShowDialog();
        }

        private void btnIClearTodayData_Click(object sender, EventArgs e)
        {
            if (dal.ClearData() == true)
            {
                txtLog.AppendText(DateTime.Now + ":数据清除成功。\r\n");
            }
            else
            {
                txtLog.AppendText(DateTime.Now + ":数据清除失败！！\r\n");
            }
        }

        private void FrmMain_Load(object sender, EventArgs e)
        {
            this.Text = "【售后查询系统】程序版本号:" + Assembly.GetExecutingAssembly().GetName().Version.ToString() + "    欢迎您:" + setting.UserRole;
            SysSettingDal dal = new SysSettingDal();
            DataTable dt = dal.GetDTFromSysSetting();
            try
            {
                txtSOP.Text = Convert.ToString(dt.Rows[0]["SOP"]);
            }
            catch (Exception)
            {
            }
            

            if (setting.UserRole=="临时账户")
            {
                btnAreaSetting.Visible = true;
                btnFifterKeyWords.Visible = true;
            }
            if (setting.UserRole =="管理员")
            {
                btnAreaSetting.Visible = true;
                btnUserManger.Visible = true;
                btnSysSetting.Visible = true;
                btnFifterKeyWords.Visible = true;
            }
        }

        private void btnUserManger_Click(object sender, EventArgs e)
        {
            new FrmUserManger().ShowDialog();
        }

        private void btnSysSetting_Click(object sender, EventArgs e)
        {
            new FrmSysSetting().ShowDialog();
        }

        private void btnAreaSetting_Click(object sender, EventArgs e)
        {
            //new FrmAreaSetting().ShowDialog();
            string filePath = Path.Combine(Application.StartupPath, "地区排序设置.txt");
            if (!File.Exists(filePath))
            {
                File.WriteAllText(filePath, "");
            }

            if (MessageBox.Show("程序即将打开地区排序设置.txt,请手动排序后点击保存。是否打开地区排序设置？", "提示:", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == System.Windows.Forms.DialogResult.Yes)
            {
                txtLog.AppendText(DateTime.Now + ":打开地区排序设置.txt中...\r\n");
                try
                {
                    System.Diagnostics.Process.Start(filePath);
                    txtLog.AppendText(DateTime.Now + ":打开地区排序设置.txt中成功,请编辑后及时保存\r\n");
                }
                catch (Exception ex)
                {
                    txtLog.AppendText(DateTime.Now + ":打开地区排序设置.txt中失败,错误代码:143:"+ex.Message+"\r\n");
                }
            }
        }

        private void FrmMain_FormClosing(object sender, FormClosingEventArgs e)
        {
            Application.Exit();
        }

        private void 用户切换ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Application.Restart();
        }

        private void 退出ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void 密码修改ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            new FrmModifyPwd().ShowDialog();
        }

        private void btnSearch_Click(object sender, EventArgs e)
        {
            FrmSearch f = new FrmSearch();
            f.Show();
            this.Hide();
            //new FrmSearch().ShowDialog();
        }

        private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            new FrmAbout().ShowDialog();
        }

        private void btnFifterKeyWords_Click(object sender, EventArgs e)
        {
            new FrmFitterKeyWords().ShowDialog();
        }
    }
}
